package com.thams.servlet;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.context.support.WebApplicationContextUtils;

import com.thams.entity.SoSodetails;
import com.thams.entity.SoSomain;
import com.thams.service.i.IU8Service;
import com.thams.util.MessagePrinter;
import com.thams.util.ParseRequestUtils;

public class SalesOrderServlet extends HttpServlet {

	private static IU8Service service;
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String forward = request.getParameter("forward");
		if (null == forward || "".equals(forward)) {
			MessagePrinter.printErrorMessage("请检查访问参数是否正确",request, response);
			return;
		}
		service = (IU8Service) WebApplicationContextUtils
				.getRequiredWebApplicationContext(
						request.getSession().getServletContext()).getBean(
						"u8Service");
		if (forward.equals("addSoSodetails")) {
			addSoSodetails(request, response);
			return;
		}
		if (forward.equals("addSoSomain")) {
			addSoSomain(request, response);
		}
		
	}
	private void addSoSomain(HttpServletRequest request,
			HttpServletResponse response) {
		SoSomain somain = new SoSomain();
		somain = (SoSomain) ParseRequestUtils.parseRequest(request, somain);
		try {
			//set functions
			String code = service.insertSoSomain(somain);
			MessagePrinter.printSuccessMessage(code,request, response);
			
		} catch (Exception e) {
			e.printStackTrace();
			MessagePrinter.printErrorMessage("添加主订单出错,请检查是否包含空数据或者已存在数据!",request,
					response);
		}
	}
	private void addSoSodetails(HttpServletRequest request,
			HttpServletResponse response) {
		SoSodetails details = new SoSodetails();
		details = (SoSodetails) ParseRequestUtils.parseRequest(request, details);
		//set functions
		try {
			service.insertSoSodetails(details);
			MessagePrinter.printSuccessMessage("添加订单详情成功",request, response);
		} catch (Exception e) {
			e.printStackTrace();
			MessagePrinter.printErrorMessage("添加订单详情出错,请检查是否包含空数据或者已存在数据!",request,
					response);
		}
	}
	

}
